import { queryBrandList, queryProductList, queryTotal, queryStatisticalIndex, queryDeviceFireHost, queryDeviceSmartSmoke, queryDeviceWisdomElectriccity, queryDeviceWisdomElectricityLogAll, queryDeviceOutdoorFireHydrant, queryDeviceCamera, queryTransmission, queryDeviceFireHostChild, queryDeviceOutdoorFireHydrantLog, queryDeviceWisdomElectricityLog, queryDeviceFlame } from './service';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';
import XLSX from 'xlsx';
import { queryDictList } from '@/services/dict';
import { exportExcl } from '@/utils/xlsx';

export default {
  namespace: 'analyse_device',

  state: {
    total: {},
    details: [],
    water: {}
  },

  effects: {
    *queryTotal(_, { call, put }) {
      const response = yield call(queryTotal);
      yield put({ type: 'setTotal', payload: response });
    },
    *queryStatisticalIndex(_, { call, put }) {
      const response = yield call(queryStatisticalIndex);
      yield put({ type: 'setStatisticalIndex', payload: response });
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *updateFireHostCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setFireHostCond', payload: cond });
      yield put({ type: 'queryDeviceFireHost', payload })
    },
    *queryDeviceFireHost({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.fireHostCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceFireHost, params);
      yield put({ type: 'setDeviceFireHost', payload: { data: list, dataCnt: +total } });
    },
    *updateSmartSmokeCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setSmartSmokeCond', payload: cond });
      yield put({ type: 'queryDeviceSmartSmoke', payload })
    },
    *queryDeviceSmartSmoke({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.smartSmokeCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceSmartSmoke, params);
      yield put({ type: 'setDeviceSmartSmoke', payload: { data: list, dataCnt: +total } });
    },
    *updateWisdomElectriccityCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setWisdomElectriccityCond', payload: cond });
      yield put({ type: 'queryDeviceWisdomElectriccity', payload })
    },
    *queryDeviceWisdomElectriccity({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.wisdomElectriccityCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceWisdomElectriccity, params);
      yield put({ type: 'setDeviceWisdomElectriccity', payload: { data: list, dataCnt: +total } });
    },
    *queryDeviceWisdomElectriccityAll({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.wisdomElectriccityCond);
      const params = { ...payload, ...cond };
      params.pageSize = 100000
      const { list = [] } = yield call(queryDeviceWisdomElectriccity, params);
      const ans = list && Array.isArray(list) && list.map(item => {
        return {
          '名称': item.name,
          '安装位置': `${item.buildName}${item.floorName ? item.floorName : ''}${item.areaName ? item.areaName : ''}`,
          '设备ID': item.networkAddr,
          '接收时间': item.receiveTime,
          '运行状态': item.runStatusName,
          '电压值/V': item.voltageValue,
          '电流值/A': item.currentValue,
          '剩余电流值/mA': item.valueMap.leakage,
          '温度1/°C': item.valueMap && item.valueMap.temperature1,
          '温度2/°C': item.valueMap && item.valueMap.temperature2,
          '温度3/°C': item.valueMap && item.valueMap.temperature3,
          '温度4/°C': item.valueMap && item.valueMap.temperature4,
          '安装时间': item.installationTime,
          '联网方式': item.wisdomElectricityOnlineTypeName,
          '厂家': item.brandName,
          '型号': item.productName,
        }
      });

      exportExcl({
        XLSX,
        data: ans,
        name: "智慧用电统计",
        fileName: "智慧用电统计"
      })
    },
    *updateOutdoorFireHydrantCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setOutdoorFireHydrantCond', payload: cond });
      yield put({ type: 'queryDeviceOutdoorFireHydrant', payload })
    },
    *queryDeviceOutdoorFireHydrant({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.outdoorFireHydrantCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceOutdoorFireHydrant, params);
      yield put({ type: 'setDeviceOutdoorFireHydrant', payload: { data: list, dataCnt: +total } });
    },
    *queryDeviceOutdoorFireHydrantAll({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.outdoorFireHydrantCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceOutdoorFireHydrant, params);
      const ans = list && Array.isArray(list) && list.map(item => {
        return {
          '名称': item.name,
          '安装位置': item.installLocation,
          '设备ID': item.networkAddr,
          '接收时间': item.receiveTime,
          '运行状态': item.runStatusName,
          '水压/MPa': item.valueMap ? Object.keys(item.valueMap).filter(it => it.indexOf('pressure') === 0).map(v => item.valueMap[v]).join(';') : '/',
          '电压': item.valueMap ? Object.keys(item.valueMap).filter(it => it.indexOf('voltage') === 0).map(v => item.valueMap[v]).join(';') : '/',
          '水位': item.valueMap ? Object.keys(item.valueMap).filter(it => it.indexOf('height') === 0).map(v => item.valueMap[v]).join(';') : '/',
          '阀门状态': item.valveStatusName,
          '盖帽': item.blockShotName,
          '安装时间': item.installationTime,
          '联网方式': item.outdoorFireHydrantOnlineTypeName,
          '厂家': item.brandName,
          '型号': item.productName
        }
      });
      exportExcl({
        XLSX,
        data: ans,
        name: "智慧用水统计",
        fileName: "智慧用水统计"
      })
    },
    *updateCameraCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setCameraCond', payload: cond });
      yield put({ type: 'queryDeviceCamera', payload })
    },
    *queryDeviceCamera({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.cameraCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceCamera, params);
      yield put({ type: 'setDeviceCamera', payload: { data: list, dataCnt: +total } });
    },
    *updateTransmissionCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setTransmissionCond', payload: cond });
      yield put({ type: 'queryTransmission', payload })
    },
    *queryTransmission({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.transmissionCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryTransmission, params);
      yield put({ type: 'setTransmission', payload: { data: list, dataCnt: +total } });
    },
    *updateDeviceFireHostChildCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setDeviceFireHostChildCond', payload: cond });
      yield put({ type: 'queryDeviceFireHostChild', payload })
    },
    *queryDeviceFireHostChild({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.fireHostChildCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceFireHostChild, params);
      yield put({ type: 'setDeviceFireHostChild', payload: { data: list, dataCnt: +total } });
    },
    *updateDeviceFlameCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setDeviceFlameCond', payload: cond });
      yield put({ type: 'queryDeviceFlame', payload })
    },
    *queryDeviceFlame({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.deviceFlameCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceFlame, params);
      yield put({ type: 'setDeviceFlame', payload: { data: list, dataCnt: +total } });
    },
    *updateDeviceOutdoorFireHydrantLogCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setDeviceOutdoorFireHydrantLogCond', payload: cond });
      yield put({ type: 'queryDeviceOutdoorFireHydrantLog', payload })
    },
    *queryDeviceOutdoorFireHydrantLog({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.outdoorFireHydrantLogCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceOutdoorFireHydrantLog, params);
      yield put({ type: 'setDeviceOutdoorFireHydrantLog', payload: { data: list, dataCnt: +total } });
    },
    *updateDeviceWisdomElectricityLogCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setDeviceWisdomElectricityLogCond', payload: cond });
      yield put({ type: 'queryDeviceWisdomElectricityLog', payload })
    },
    *queryDeviceWisdomElectricityLog({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.deviceWisdomElectricityLogCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      const { list = [], total } = yield call(queryDeviceWisdomElectricityLog, params);
      yield put({ type: 'setDeviceWisdomElectricityLog', payload: { data: list, dataCnt: +total } });
    },
    *queryDeviceWisdomElectricityLogAll({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_device.deviceWisdomElectricityLogCond);
      const params = { ...payload, ...cond };
      params.pageSize = 1000000;
      const { list = [], total } = yield call(queryDeviceWisdomElectricityLogAll, params);
      yield put({ type: 'setDeviceWisdomElectricityLogAll', payload: { data: list, dataCnt: +total } });
    },
    *queryInfo(_, { put }) {
      yield put({ type: 'queryTotal' });
      yield put({ type: 'queryStatisticalIndex' });
    },
    *deviceBrand({ payload }, { call, put }) {
      const response = yield call(queryBrandList);
      yield put({ type: 'getBrandData', payload: response })
    },
    *optionProduct({ payload }, { call, put }) {
      const response = yield call(queryProductList, payload);
      yield put({ type: 'getProductData', payload: response })
    }
  },

  reducers: {
    setTotal(state, action) {
      return {
        ...state,
        total: action.payload
      }
    },
    setStatisticalIndex(state, action) {
      return {
        ...state,
        statisticalIndex: action.payload
      }
    },
    setFireHostCond(state, action) {
      return {
        ...state,
        fireHostCond: action.payload
      }
    },
    setDeviceFireHost(state, action) {
      return {
        ...state,
        deviceFireHost: action.payload
      }
    },
    setSmartSmokeCond(state, action) {
      return {
        ...state,
        smartSmokeCond: action.payload
      }
    },
    setDeviceSmartSmoke(state, action) {
      return {
        ...state,
        deviceSmartSmoke: action.payload
      }
    },
    setWisdomElectriccityCond(state, action) {
      return {
        ...state,
        wisdomElectriccityCond: action.payload
      }
    },
    setDeviceWisdomElectriccity(state, action) {
      return {
        ...state,
        deviceWisdomElectriccity: action.payload
      }
    },
    setOutdoorFireHydrantCond(state, action) {
      return {
        ...state,
        outdoorFireHydrantCond: action.payload
      }
    },
    setDeviceOutdoorFireHydrant(state, action) {
      return {
        ...state,
        deviceOutdoorFireHydrant: action.payload
      }
    },
    setDeviceOutdoorFireHydrantAll(state, action) {
      return {
        ...state,
        deviceOutdoorFireHydrantAll: action.payload
      }
    },
    setCameraCond(state, action) {
      return {
        ...state,
        cameraCond: action.payload
      }
    },
    setDeviceCamera(state, action) {
      return {
        ...state,
        deviceCamera: action.payload
      }
    },
    setTransmissionCond(state, action) {
      return {
        ...state,
        transmissionCond: action.payload
      }
    },
    setTransmission(state, action) {
      return {
        ...state,
        transmission: action.payload
      }
    },
    setDeviceFireHostChildCond(state, action) {
      return {
        ...state,
        fireHostChildCond: action.payload
      }
    },
    setDeviceFireHostChild(state, action) {
      return {
        ...state,
        deviceFireHostChild: action.payload
      }
    },
    setDeviceFlameCond(state, action) {
      return {
        ...state,
        deviceFlameCond: action.payload
      }
    },
    setDeviceFlame(state, action) {
      return {
        ...state,
        deviceFlame: action.payload
      }
    },
    setDeviceOutdoorFireHydrantLogCond(state, action) {
      return {
        ...state,
        outdoorFireHydrantLogCond: action.payload
      }
    },
    setDeviceOutdoorFireHydrantLog(state, action) {
      return {
        ...state,
        outdoorFireHydrantLog: action.payload
      }
    },
    setDeviceWisdomElectricityLogCond(state, action) {
      return {
        ...state,
        deviceWisdomElectricityLogCond: action.payload
      }
    },
    setDeviceWisdomElectricityLog(state, action) {
      return {
        ...state,
        deviceWisdomElectricityLog: action.payload
      }
    },
    setDeviceWisdomElectricityLogAll(state, action) {
      return {
        ...state,
        deviceWisdomElectricityLogAll: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    getBrandData(state, action) {
      return {
        ...state,
        brandData: action.payload
      }
    },
    getProductData(state, action) {
      return {
        ...state,
        productData: action.payload
      }
    }
  }
}
